package com.zeta.eduservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduTeacher;
import com.zeta.eduservice.entity.LoginVo;
import com.zeta.eduservice.entity.RegisterVo;
import com.zeta.eduservice.entity.UcenterMember;
import com.zeta.eduservice.entity.infoVo;
import com.zeta.eduservice.mapper.UcenterMemberMapper;
import com.zeta.eduservice.service.EduTeacherService;
import com.zeta.eduservice.service.UcenterMemberService;
import com.zeta.exceptionhandler.GuliException;

import com.zeta.utils.JwtUtils;
import com.zeta.utils.MD5;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

	@Autowired
	EduTeacherService teaservice;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录的方法
    @Override
    public String login(LoginVo member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登录失败1");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            throw new GuliException(20001,"登录失败2");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
//        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
//            throw new GuliException(20001,"登录失败3");
//        }

        if(!password.equals(mobileMember.getPassword())) {
          throw new GuliException(20001,"登录失败3");
      }
        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new GuliException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //注册的方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001,"注册失败");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)) {
            throw new GuliException(20001,"注册失败");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new GuliException(20001,"注册失败");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码需要加密的
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
    }

	@Override
	public Integer countRegisterByDay(String day) {
		return baseMapper.countRegisterDay(day);
	}

	@Override
	public void binding(infoVo info) {
		UcenterMember ucenterMember = new UcenterMember();
	BeanUtils.copyProperties(info, ucenterMember);
	
//		R re=eduClient.getByNumber(info.getNumber(),info.getAcademy());
//	 Object object = re.getData().get("teacher");
	QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<EduTeacher>();
	queryWrapper.eq("number", info.getNumber());
	queryWrapper.eq("academy", info.getAcademy());
EduTeacher teacher = teaservice.getOne(queryWrapper);


		 if(teacher!=null) {
			  teacher.setUserid(info.getId());
			 
//			eduClient.updateById(teacher.getId(), teacher);
			
		    teacher.setId(teacher.getId());
		    teaservice.updateById(teacher);
		 }else {
			 throw new GuliException(20001, "没有此学号信息啥");
		 }
		 ucenterMember.setTeacher(teacher.getId());
			int updateById = baseMapper.updateById(ucenterMember);
	}


}
